/**
* @license
* Licensed Materials - Property of IBM
* 5725-G92 (C) Copyright IBM Corp. 2006, 2013. All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*/

function wlCommonInit(){

	/*
	 * Application is started in offline mode as defined by a connectOnStartup property in initOptions.js file.
	 * In order to begin communicating with Worklight Server you need to either:
	 * 
	 * 1. Change connectOnStartup property in initOptions.js to true. 
	 *    This will make Worklight framework automatically attempt to connect to Worklight Server as a part of application start-up.
	 *    Keep in mind - this may increase application start-up time.
	 *    
	 * 2. Use WL.Client.connect() API once connectivity to a Worklight Server is required. 
	 *    This API needs to be called only once, before any other WL.Client methods that communicate with the Worklight Server.
	 *    Don't forget to specify and implement onSuccess and onFailure callback functions for WL.Client.connect(), e.g:
	 *    
	  
	     */
	WL.Client.connect({
	     		onSuccess: onConnectSuccess,
	     		onFailure: onConnectFailure
	     });
	      
	 
	
	
	// Common initialization code goes here

}

var pageHistory = pageHistory || {} ;
pageHistory.first_path = null;

pageHistory.navigateToPage = function (page_id,options){
	if (typeof page_id == "undefined") 
		window.location = pageHistory.first_path;
	else {
	url = page_id + ".html";
	options = (typeof options == "undefined" )?{}:options;
	window.history.pushState({ "page_id" : page_id}, "" , pageHistory.first_path + "#" + page_id);
	options.changeHash = false;
	$.mobile.changePage(url, options );
	}
};

pageHistory.initialize = function (){
	pageHistory.first_path = window.location.pathname ;
	window.onpopstate = function(event){
		pageHistory.navigateToPage(event.state.page_id , {});
	};
};

pageHistory.back = function (){
	window.history.back();
};

function  onConnectSuccess(){
	//listAllProjects();
	WL.Logger.debug("Redmine::wlCommonInit::connectSuccess");
	pageHistory.initialize();
	pageHistory.navigateToPage("allProjects",{transition: "pop"});
	

}

function onConnectFailure(){
	WL.Logger.debug("Redmine::wlCommonInit::connectFailure");

	pageHistory.navigateToPage("workOffline",{transition: "flip"});
}
/*
<input type="button" value="Logout" onclick="WL.Client.logout('CustomAuthenticatorRealm',{onSuccess: WL.Client.reloadApp})" />
*/